home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / What's New? / Sample Code / Graphics 2D / QuickDraw FX / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  13.4 KB  |  547 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        main.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: EL
  7.     
  8.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  20.                                             for demonstration purposes.
  21.                 7/13/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25.  
  26. #include "FX.h"
  27. #include <Fonts.h>
  28. #include <TextEdit.h>
  29. #include <Dialogs.h>
  30. #include <Resources.h>
  31. #include <TextUtils.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34.  
  35. /* Global Variable Definitions */
  36.  
  37. WindowPtr        gWindow;
  38. GWorldPtr        gGWorld = nil;
  39. int                gCurrentExample = 11;
  40.  
  41. itemType    tItem[numTItems];
  42. itemType    aItem[numAItems];
  43. itemType    cItem[numCItems];
  44. itemType    dItem[numDItems];
  45. itemType    mItem[numMItems];
  46. itemType    bItem[numBItems];
  47. itemType    pItem[numPItems];
  48. itemType    lItem[numLItems];
  49.  
  50. itemSettings    settings;
  51.  
  52. void main()
  53. {
  54.     //MaxApplZone();
  55.  
  56.     initMac();
  57.     
  58.     createOffscreen( 1 );
  59.     createWindow();
  60.  
  61.     resetItems();
  62.     defineItems();
  63.  
  64.     eventLoop();
  65. }
  66.  
  67. void initMac()
  68. {
  69.     /*InitGraf( &qd.thePort );
  70.     InitFonts();
  71.     InitWindows();
  72.     InitMenus();
  73.     TEInit();
  74.     InitDialogs( nil );*/
  75.     InitCursor();
  76.     FlushEvents( 0, everyEvent );
  77.     
  78.     setUpMenus();
  79. }
  80.  
  81. void createOffscreen(int pictItem)
  82. {
  83.     PicHandle    pict;
  84.     Rect        rect;
  85.     CGrafPtr    currentPort;
  86.     GDHandle    currentDevice;
  87.     
  88.     if (gGWorld != nil)
  89.         DisposeGWorld( gGWorld );
  90.     
  91.     pict = (PicHandle)GetResource( 'PICT', pictItem + 127 );
  92.     
  93.     rect = (**pict).picFrame;
  94.     
  95.     GetGWorld( ¤tPort, ¤tDevice );
  96.     NewGWorld( &gGWorld, 32, &rect, nil, nil, 0 );
  97.         
  98.     //LockPixels( (*gGWorld).portPixMap );
  99.     LockPixels( GetPortPixMap(gGWorld));
  100.     SetGWorld( gGWorld, nil );
  101.  
  102.     DrawPicture( pict, &rect );
  103.     
  104.     SetGWorld( currentPort, currentDevice );
  105.     
  106.     ReleaseResource( (Handle)pict );
  107. }
  108.  
  109. void createWindow()
  110. {
  111.     int        width, height;
  112.     int        left, top;
  113.     Rect    rect;
  114.     Rect    tempRect1;
  115.     BitMap    bitMap;
  116.     
  117.     //width = 58 + ((*gGWorld).portRect.right - (*gGWorld).portRect.left) * 2;
  118.     //height = 230 + (*gGWorld).portRect.bottom - (*gGWorld).portRect.top;
  119.     
  120.     GetPortBounds(gGWorld, &tempRect1);
  121.     
  122.     width = 58 + (tempRect1.right - tempRect1.left) * 2;
  123.     height = 230 + tempRect1.bottom - tempRect1.top;
  124.     
  125.     //left = (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - width) / 2);
  126.     //top = (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - height) / 2) + 20;
  127.     
  128.     tempRect1 = GetQDGlobalsScreenBits(&bitMap)->bounds;
  129.     
  130.     left = (((tempRect1.right - tempRect1.left) - width) / 2);
  131.     top = (((tempRect1.bottom - tempRect1.top) - height) / 2) + 20;
  132.  
  133.     SetRect( &rect, left, top, left + width, top + height );
  134.     gWindow = NewCWindow( 0L, &rect, "\pQuickDraw Special Effects", true, noGrowDocProc,
  135.                             (WindowPtr)-1L, true, 0 );
  136.                             
  137.     //SetPort( gWindow );
  138.     SetPortWindowPort( gWindow );
  139. }
  140.  
  141. void resetItems()
  142. {
  143.     settings.tItem = 1;
  144.     settings.aItem = 1;
  145.     settings.cItem = 1;
  146.     settings.mItem = 1;
  147.     settings.dItem = 1;
  148.     settings.pItem = 1;
  149.     settings.lItem = 1;
  150. }
  151.  
  152. void defineItems()
  153. {
  154.     int        i;
  155.     int        col, row;
  156.     
  157.     char    tNames[numTItems][15] = {    "srcCopy", "srcOr", "srcXor", "srcBic",
  158.                                         "notSrcCopy", "notSrcOr", "notSrcXor", "notSrcBic"    };
  159.     char    aNames[numAItems][15] = {    "blend", "addPin", "addOver", "subPin",
  160.                                         "adMax", "subMin", "adMin", "transparent"    };
  161.     char    cNames[numCItems][15] = {    "None", "Inverse", "FG Only", "BG Only"    };
  162.     char    dNames[numDItems][15] = {    "Dithering OFF", "Dithering ON"    };
  163.     char    mNames[numMItems][15] = {    "None", "Search Proc", "ColorTable"    };
  164.     char    bNames[numBItems][15] = {    "White", "Black", "White&Black"    };
  165.     char    pNames[numPItems][15] = {    "SeedCFill", "SeedFill"    };
  166.     char    lNames[numLItems][15] = {    "CalcCMask", "CalcMask"    };
  167.     Rect    tempRect1;
  168.     
  169.     settings.bItem = 1;
  170.     
  171.     col = 15;
  172.     //row = (*gWindow).portRect.bottom - 157;
  173.     row = GetPortBounds(GetWindowPort(gWindow), &tempRect1)->bottom - 157;
  174.     
  175.     for (i = 0; i < numTItems; i++)
  176.     {
  177.         strcpy(P2CStr(tItem[i].label),tNames[i]);
  178.         C2PStr((char *)tItem[i].label);
  179.         SetRect( &tItem[i].rect, col + 40, row + (i * 15),
  180.                     col + 40 + 90, row + ((i + 1) * 15) );
  181.     }
  182.     
  183.     for (i = 0; i < numAItems; i++)
  184.     {
  185.         strcpy(P2CStr(aItem[i].label),aNames[i]);
  186.         C2PStr((char *)aItem[i].label);
  187.         SetRect( &aItem[i].rect, col + 140, row + (i * 15),
  188.                     col + 140 + 90, row + ((i + 1) * 15) );
  189.     }
  190.     
  191.     for (i = 0; i < numCItems; i++)
  192.     {
  193.         strcpy(P2CStr(cItem[i].label),cNames[i]);
  194.         C2PStr((char *)cItem[i].label);
  195.         SetRect( &cItem[i].rect, col + 240, row + (i * 15),
  196.                     col + 240 + 90, row + ((i + 1) * 15) );
  197.     }
  198.     
  199.     for (i = 0; i < numDItems; i++)
  200.     {
  201.         strcpy(P2CStr(dItem[i].label),dNames[i]);
  202.         C2PStr((char *)dItem[i].label);
  203.         SetRect( &dItem[i].rect, col + 240, row + 90 + (i * 15),
  204.                     col + 240 + 90, row + 90 + ((i + 1) * 15) );
  205.     }
  206.         
  207.     for (i = 0; i < numMItems; i++)
  208.     {
  209.         strcpy(P2CStr(mItem[i].label),mNames[i]);
  210.         C2PStr((char *)mItem[i].label);
  211.         SetRect( &mItem[i].rect, col + 340, row + (i * 15),
  212.                     col + 340 + 90, row + ((i + 1) * 15) );
  213.     }
  214.     
  215.     for (i = 0; i < numBItems; i++)
  216.     {
  217.         strcpy(P2CStr(bItem[i].label),bNames[i]);
  218.         C2PStr((char *)bItem[i].label);
  219.         SetRect( &bItem[i].rect, col + 340, row + 75 + (i * 15),
  220.                     col + 340 + 90, row + 75 + ((i + 1) * 15) );
  221.     }
  222.     
  223.     for (i = 0; i < numPItems; i++)
  224.     {
  225.         strcpy(P2CStr(pItem[i].label),pNames[i]);
  226.         C2PStr((char *)pItem[i].label);
  227.         SetRect( &pItem[i].rect, col + 440, row + (i * 15),
  228.                     col + 440 + 90, row + ((i + 1) * 15) );
  229.     }
  230.     
  231.     for (i = 0; i < numLItems; i++)
  232.     {
  233.         strcpy(P2CStr(lItem[i].label),lNames[i]);
  234.         C2PStr((char *)lItem[i].label);
  235.         SetRect( &lItem[i].rect, col + 440, row + 60 + (i * 15),
  236.                     col + 440 + 90, row + 60 + ((i + 1) * 15) );
  237.     }
  238. }
  239.  
  240. void updateWindow()
  241. {
  242.     long    ticks;
  243.     
  244.     drawBackground();
  245.     drawAllItems();
  246.     drawExampleName();
  247.     drawSourceImage();
  248.     
  249.     ticks = drawFXImage();
  250.     drawTime( ticks );
  251. }
  252.  
  253. void drawBackground()
  254. {
  255.     Rect        rect;
  256.     RGBColor    color;
  257.     Rect        tempRect1;
  258.     
  259.     color.red = color.green = color.blue = 8700;
  260.     
  261.     RGBForeColor( &color );
  262.     //PaintRect( &(*gWindow).portRect );
  263.     PaintRect( GetPortBounds(GetWindowPort(gWindow), &tempRect1));
  264.     
  265.     TextFont( kFontIDTimes );
  266.     TextMode( srcOr );
  267.     TextSize( 24 );
  268.     
  269.     drawName( 85, 22, "\pSource Image" );
  270.     //drawName( (*gWindow).portRect.right - 215, 22, "\pNew Image" );
  271.     drawName( GetPortBounds(GetWindowPort(gWindow), &tempRect1)->right - 215, 22, "\pNew Image" );
  272.     
  273.     /*SetRect( &rect, 15, (*gWindow).portRect.bottom - 180, 
  274.                 (*gWindow).portRect.right - 15, (*gWindow).portRect.bottom - 30 );*/
  275.     GetPortBounds(GetWindowPort(gWindow), &tempRect1);
  276.     SetRect( &rect, 15, tempRect1.bottom - 180, tempRect1.right - 15, tempRect1.bottom - 30);
  277.     drawDeepBox( &rect );
  278.     
  279.     TextSize( 12 );
  280.     
  281.     drawName( tItem[0].rect.left, tItem[0].rect.top - 8, "\pTransfer Mode" );
  282.     drawName( aItem[0].rect.left, aItem[0].rect.top - 8, "\pArithmetic Mode" );
  283.     drawName( cItem[0].rect.left, cItem[0].rect.top - 8, "\pColorization" );
  284.     drawName( dItem[0].rect.left, dItem[0].rect.top - 8, "\pDither" );
  285.     drawName( mItem[0].rect.left, mItem[0].rect.top - 8, "\pColor Mapping" );
  286.     drawName( bItem[0].rect.left, bItem[0].rect.top - 8, "\pDestination" );
  287.     drawName( pItem[0].rect.left, pItem[0].rect.top - 8, "\pPaint Bucket" );
  288.     drawName( lItem[0].rect.left, lItem[0].rect.top - 8, "\pLasso Tool" );
  289. }
  290.  
  291. void drawAllItems()
  292. {
  293.     drawTransferItems();
  294.     drawArithmeticItems();
  295.     drawColorizeItems();
  296.     drawDitherItems();
  297.     drawMappingItems();
  298.     drawDestinationItems();
  299.     drawPaintBucketItems();
  300.     drawLassoToolItems();
  301. }
  302.  
  303. void drawTransferItems()
  304. {
  305.     int            i;
  306.     Boolean        listEnabled;
  307.  
  308.     listEnabled = (gCurrentExample / 10 == transferID || gCurrentExample / 10 == customID);
  309.     
  310.     for (i = 0; i < numTItems; i++)
  311.         drawItem( tItem[i].rect.left, tItem[i].rect.top, &tItem[i].label,
  312.                     listEnabled, settings.tItem == i + 1 );
  313. }
  314.  
  315. void drawArithmeticItems()
  316. {
  317.     int            i;
  318.     Boolean        listEnabled;
  319.     
  320.     listEnabled = (gCurrentExample / 10 == arithmeticID);
  321.     
  322.     for (i = 0; i < numAItems; i++)
  323.         drawItem( aItem[i].rect.left, aItem[i].rect.top, &aItem[i].label,
  324.                     listEnabled, settings.aItem == i + 1 );
  325. }
  326.  
  327. void drawColorizeItems()
  328. {
  329.     int            i;
  330.     Boolean        listEnabled;
  331.     
  332.     listEnabled = (gCurrentExample / 10 == colorizationID || gCurrentExample / 10 == customID);
  333.         
  334.     for (i = 0; i < numCItems; i++)
  335.         drawItem( cItem[i].rect.left, cItem[i].rect.top, &cItem[i].label,
  336.                     listEnabled, settings.cItem == i + 1 );
  337. }
  338.  
  339. void drawDitherItems()
  340. {
  341.     int            i;
  342.     Boolean        listEnabled;
  343.     
  344.     listEnabled = (gCurrentExample / 10 == ditherID || gCurrentExample / 10 == customID);
  345.         
  346.     for (i = 0; i < numDItems; i++)
  347.         drawItem( dItem[i].rect.left, dItem[i].rect.top, &dItem[i].label,
  348.                     listEnabled, settings.dItem == i + 1 );
  349. }
  350.  
  351. void drawMappingItems()
  352. {
  353.     int            i;
  354.     Boolean        listEnabled;
  355.     
  356.     listEnabled = (gCurrentExample / 10 == searchProcID || gCurrentExample / 10 == customID);
  357.     
  358.     for (i = 0; i < numMItems; i++)
  359.         drawItem( mItem[i].rect.left, mItem[i].rect.top, &mItem[i].label,
  360.                     listEnabled, settings.mItem == i + 1 );
  361. }
  362.  
  363. void drawDestinationItems()
  364. {
  365.     int            i;
  366.     
  367.     for (i = 0; i < numBItems; i++)
  368.         drawItem( bItem[i].rect.left, bItem[i].rect.top, &bItem[i].label,
  369.                     true, settings.bItem == i + 1 );
  370. }
  371.  
  372. void drawPaintBucketItems()
  373. {
  374.     int            i;
  375.     Boolean        listEnabled;
  376.     
  377. //    listEnabled = (gCurrentExample / 10 == paintBucketID);
  378.     listEnabled = false;
  379.     
  380.     for (i = 0; i < numPItems; i++)
  381.         drawItem( pItem[i].rect.left, pItem[i].rect.top, &pItem[i].label,
  382.                     listEnabled, settings.pItem == i + 1 );
  383. }
  384.  
  385. void drawLassoToolItems()
  386. {
  387.     int            i;
  388.     Boolean        listEnabled;
  389.     
  390. //    listEnabled = (gCurrentExample / 10 == lassoID);
  391.     listEnabled = false;
  392.     
  393.     for (i = 0; i < numLItems; i++)
  394.         drawItem( lItem[i].rect.left, lItem[i].rect.top, &lItem[i].label,
  395.                     listEnabled, settings.lItem == i + 1 );
  396. }
  397.  
  398. void drawName(int left, int top, Str255 name )
  399. {
  400.     RGBColor    color;
  401.     
  402.     ForeColor( blackColor );
  403.     MoveTo( left + 1, top + 1 );
  404.     DrawString( name );
  405.     
  406.     color.red = color.green = 0xffff;
  407.     color.blue = 40000;
  408.     RGBForeColor( &color );
  409.     
  410.     MoveTo( left, top );
  411.     DrawString( name );
  412. }
  413.  
  414. void drawItem(int left, int top, Str255 *label, Boolean listEnabled, Boolean itemEnabled )
  415. {
  416.     Rect        rect;
  417.     RGBColor    color, markColor;
  418.     
  419.     if (listEnabled)
  420.         color.red = color.green = color.blue = 0xffff;
  421.     else
  422.         color.red = color.green = color.blue = 0xffff / 2;
  423.         
  424.     SetRect( &rect, left, top + 2, left + 12, top + 14 );
  425.     ForeColor( blackColor );
  426.     FrameOval( &rect );
  427.     
  428.     OffsetRect( &rect, -1, -1 );
  429.     RGBForeColor( &color );
  430.     FrameOval( &rect );
  431.                 
  432.     if (listEnabled && itemEnabled)
  433.         markColor.red = markColor.green = markColor.blue = 0xffff;
  434.     else if (itemEnabled)
  435.         markColor.red = markColor.green = markColor.blue = 0xffff / 2;
  436.     else
  437.         markColor.red = markColor.green = markColor.blue = 8700;
  438.     
  439.     InsetRect( &rect, 3, 3 );
  440.     RGBForeColor( &markColor );
  441.     PaintOval( &rect );
  442.     
  443.     ForeColor( blackColor );
  444.     MoveTo( left + 16, top + 11 );
  445.     DrawString( (unsigned char*)label );
  446.     
  447.     RGBForeColor( &color );
  448.     MoveTo( left + 15, top + 10 );
  449.     DrawString( (unsigned char*)label );
  450. }
  451.  
  452. void drawDeepBox( Rect *rect )
  453. {
  454.     Rect        box;
  455.     
  456.     box = *rect;
  457.     box.right += 2;
  458.     box.bottom += 2;
  459.     
  460.     ForeColor( blackColor );
  461.     FrameRect( &box );
  462.     
  463.     OffsetRect( &box, -1, -1 );
  464.     ForeColor( whiteColor );
  465.     FrameRect( &box );
  466. }
  467.  
  468. void drawExampleName()
  469. {
  470.     int            left, top;
  471.     Rect        rect;
  472.     Str255        label = "\pCurrent Example:  ";
  473.     Str255        name[9] = {    "\p TRANSFER MODES", "\pARITHMETIC MODES", "\p   DITHERING",
  474.                             "\p  COLORIZATION", "\p  COLOR MAPPING", "\p  PAINT BUCKET",
  475.                             "\p   LASSO TOOL", "\pPIXEL AVERAGING", "\p    CUSTOM"    };
  476.     Rect        tempRect1;
  477.     
  478.     left = 15;
  479.     //top = (*gWindow).portRect.bottom - 13;
  480.     top = GetPortBounds(GetWindowPort(gWindow), &tempRect1)->bottom - 13;
  481.     
  482.     drawName( left, top, label );
  483.     left += StringWidth( label );
  484.     
  485.     SetRect( &rect, left - 2, top - 12, left + 130, top + 4 );
  486.     eraseRect( &rect );
  487.     
  488.     drawName( left, top, name[(gCurrentExample / 10) - 1] );
  489. }
  490.  
  491. void drawTime( long ticks )
  492. {
  493.     Rect    rect;
  494.     int        left, top;
  495.     float    seconds;
  496.     char    cString[40];
  497.     Rect    tempRect1;
  498.     
  499.     //left = (*gWindow).portRect.right - 130;
  500.     //top = (*gWindow).portRect.bottom - 13;
  501.     GetPortBounds(GetWindowPort(gWindow), &tempRect1);
  502.     left = tempRect1.right - 130;
  503.     top = tempRect1.bottom - 13;
  504.     
  505.     
  506.     SetRect( &rect, left - 2, top - 12, left + 90, top + 4 );
  507.     eraseRect( &rect );
  508.     
  509.     seconds = (float)ticks / 60.0;
  510.     sprintf( &cString[0], "Draw Time: %03.03f secs", seconds );
  511.     
  512.     drawName( left, top, C2PStr( cString ) );
  513. }
  514.  
  515. void eraseRect( Rect *rect )
  516. {
  517.     RGBColor    color;
  518.     
  519.     color.red = color.green = color.blue = 8700;
  520.     
  521.     RGBForeColor( &color );
  522.     PaintRect( rect );
  523. }
  524.  
  525. void drawSourceImage()
  526. {
  527.     Rect    rect;
  528.     Rect    outlineRect;
  529.     Rect    tempRect1;
  530.  
  531.     /*SetRect( &rect, 20, 37, 20 + (*gGWorld).portRect.right,
  532.             37 + (*gGWorld).portRect.bottom );*/
  533.     GetPortBounds(gGWorld, &tempRect1);        
  534.     SetRect( &rect, 20, 37, 20 + tempRect1.right, 37 + tempRect1.bottom);
  535.  
  536.     outlineRect = rect;
  537.     InsetRect( &outlineRect, -5, -5 );
  538.     drawDeepBox( &outlineRect );
  539.     
  540.     ForeColor( blackColor );
  541.     BackColor( whiteColor );
  542.     
  543.     /*CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
  544.                 &(**(*gGWorld).portPixMap).bounds, &rect, srcCopy, nil );*/
  545.     CopyBits( (BitMap *)(*(GetPortPixMap(gGWorld))), GetPortBitMapForCopyBits(GetWindowPort(gWindow)),
  546.                 &((**(GetPortPixMap(gGWorld))).bounds), &rect, srcCopy, nil );
  547. }